﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace TYG.GateWay
{
    using DotNetty.Codecs.Protobuf;
    using DotNetty.Handlers.Logging;
    using DotNetty.Handlers.Tls;
    using DotNetty.Transport.Bootstrapping;
    using DotNetty.Transport.Channels;
    using DotNetty.Transport.Channels.Sockets;
    using System;
    using System.IO;
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    using System.Threading.Tasks;

    class Program
    {
        static async Task RunServerAsync()
        {
            // TianYou.SetConsoleLogger();

            var bossGroup = new MultithreadEventLoopGroup(1);
            var workerGroup = new MultithreadEventLoopGroup();
            var SERVER_HANDLER = new SecureChatServerHandler();
            

         
            try
            {
                var bootstrap = new ServerBootstrap();
                bootstrap
                    .Group(bossGroup, workerGroup)
                    .Channel<TcpServerSocketChannel>()
                    .Option(ChannelOption.SoBacklog, 100)
                    .Handler(new LoggingHandler(DotNetty.Handlers.Logging.LogLevel.INFO))
                    .ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel =>
                    {
                        IChannelPipeline pipeline = channel.Pipeline;
                       
                        //pipeline.AddLast(STRING_ENCODER, STRING_DECODER, SERVER_HANDLER);
                        //pipeline.AddLast(STRING_ENCODER, new ProtobufDecoder(ChatMessage.Parser), SERVER_HANDLER);
                        //pipeline.AddLast(STRING_ENCODER, new ProtobufDecoder(SendFrameRequest.Parser), SERVER_HANDLER);
                        pipeline.AddLast(STRING_ENCODER);
                        pipeline.AddLast(SERVER_HANDLER);
                       // pipeline.AddLast(STRING_ENCODER, new ProtobufDecoder(SendFrameRequest.Parser), SERVER_HANDLER);
                    }));
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9999);
                IChannel bootstrapChannel = await bootstrap.BindAsync(iep);
                Console.WriteLine(bootstrapChannel.RemoteAddress);
                Console.WriteLine(bootstrapChannel.LocalAddress);
                Console.WriteLine("游戏服务器已启动");
                //Console.ReadLine();
                //await bootstrapChannel.CloseAsync();
            }
            finally
            {
                //Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
            }
        }

        static void Main() => RunServerAsync().Wait();
    }
}